import com.sun.prism.sw.SWPipeline;

import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: XMQ
 * Date: 2022-05-23
 * Time: 9:18
 */
//java实现swap方法+堆排序+几道leetcode调试
class Swap{
    public int val;
}
class Num{
    public int[] nums;
}
public class Test {
    //md,这才对嘛
    public static void swap(int[] nums,int i,int j){
        int tmp = nums[i];
        nums[i] = nums[j];
        nums[j] = tmp;
    }
    public static void main8(String[] args) {
        int[] nums = {3,2,3,2,1,2};
        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; j < nums.length-i-1; j++) {
                if(nums[j] > nums[j+1]){
                    swap(nums,j,j+1);
                }
            }
        }
        System.out.println(Arrays.toString(nums));
    }
    //妈的终于写好了
    public static void swap(Num num,int i ,int j){
        int tmp = num.nums[i];
        num.nums[i] = num.nums[j];
        num.nums[j] = tmp;
    }
    public static void main7(String[] args) {
        Num num = new Num();
        num.nums = new int[]{3,2,3,2,1,2};
        System.out.println(Arrays.toString(num.nums));
        for (int i = 0; i < num.nums.length; i++) {
            for (int j = 0; j < num.nums.length-i-1; j++) {
                if(num.nums[j] > num.nums[j+1]){
                    swap(num,j+1,j);
                }
            }
        }
        System.out.println(Arrays.toString(num.nums));
    }
    public static void swap(Swap s1,Swap s2) {
        int tmp = s1.val;
        s1.val = s2.val;
        s2.val = tmp;
    }
    public static void func(int i,int j){
        Swap swap1 = new Swap();
        Swap swap2 = new Swap();
        swap1.val = i;
        swap2.val = j;
//        System.out.println(swap1.val);
//        System.out.println(swap2.val);
        swap(swap1,swap2);
//        System.out.println(swap1.val);
//        System.out.println(swap2.val);
    }

    public static void main6(String[] args) {
        func(10,20);
    }
    public static void main5(String[] args) {
        int[] nums = {3,2,3,2,1,2};
        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; j < nums.length-i-1; j++) {
                if(nums[j] > nums[j+1]){
//                    swap(nums[j],nums[j+1]);
                }
            }
        }
        System.out.println(Arrays.toString(nums));
    }

    public static void main4(String[] args) {
        int[] nums = {3,2,3,2,1,2};
//        BubbleSort(nums);
        System.out.println(Arrays.toString(nums));
        Swap swap1 = new Swap();
        Swap swap2 = new Swap();
        for (int i = 0; i < nums.length; i++) {
            for (int j = 0; j <nums.length-i-1; j++) {
                if(nums[j] > nums[j+1]){
                    swap1.val = nums[j];
                    swap2.val = nums[j+1];
                    swap(swap1,swap2);
                }
            }
        }
        System.out.println(Arrays.toString(nums));
    }
    //堆排序
    public static void AdjustDown(int[] nums,int root,int n){
        int parent = root;
        int child = parent*2+1;//默认左子树
        while(child < n){
            if(child + 1 < n && nums[child] < nums[child+1]){
                child += 1;
            }
            if(nums[parent] < nums[child]){
                swap(nums,parent,child);
            }else{
                break;
            }
        }
    }
    public static void Heap(int[] nums){
        //建堆 大堆-升序
        int n = nums.length;//数组长度
        for (int i = (n-1-1)/2; i > 0; i--) {
            //向下调整算法
            AdjustDown(nums,i,n);
        }
        //排序
        int end = n-1;
        while(end >= 0) {
            int tmp = nums[0];
            nums[0] = nums[end];
            nums[end] = tmp;
            AdjustDown(nums,0,end);
            end--;
        }
    }
    public static void main(String[] args) {
        int[] nums = {3,2,3,2,1,2};
        Heap(nums);
        System.out.println(Arrays.toString(nums));
    }
    public static void BubbleSort(int[] nums){
        for (int i = 0; i < nums.length; i++) {
            boolean flg = false;
            for (int j = 0; j < nums.length-i-1; j++) {
                if(nums[j] > nums[j+1]){
                    int tmp = nums[j+1];
                    nums[j+1] = nums[j];
                    nums[j] = tmp;
                    flg = true;
                }
            }
            if(!flg){
                break;
            }
        }
    }
    public static void main2(String[] args) {
        int[] nums = {3,2,3,2,2,2};
        BubbleSort(nums);
        System.out.println(Arrays.toString(nums));
    }
    //
    public static boolean divideArray(int[] nums) {
        int[] hash = new int[501];
        for(int i = 0;i < nums.length;i++){
            ++hash[ nums[i] ];
        }
        for(int i = 0;i < 500; i++){
            if((hash[i] & 1) == 1){
                return false;
            }
        }
        return true;
    }
    public static void main1(String[] args) {
        int[] nums = {3,2,3,2,2,2};
        System.out.println(divideArray(nums));
    }
}
